// pages/shop_edit/classify_list/classify_list.js
const service = require('../../../utils/service.js')
const api = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    shopInfo: '',
    shopData: '',
    idx: '',
    searchVal: '',
    classfiylist: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    let idx = '';
    if(options.index){
      idx = options.index;
    }
    this.setData({
      idx: idx,
      userInfo: userInfo
    })
    this.init()
  },

  init: function () {
    this.getShopInfo(() => {
      this.getList()
    })
  },

  // 获取店铺信息
  getShopInfo: function (cb) {
    let that = this;
    let shopData = wx.getStorageSync('shop_data');
    api.ajax(service.url('getShopDetail'), {
      userid: wx.getStorageSync('out_user_id')
    }, function (res) {
      that.setData({
        shopData: shopData,
        shopInfo: res.data
      })
      cb()
    })
  },

  // 获取分类列表
  getList: function () {
    let that = this
    api.ajax(service.url('categoryGetAll'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: that.data.shopInfo.shop_code
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          classfiylist: res.data
        })
      } else {
        api.toast(res.messagef)
      }
    })
  },

  //监听搜索输入
  getSearchVal: function(e){
    let val = e.detail.value;
    this.setData({
      searchVal: val
    })
  },

  // 搜索分类
  searchClassfiy: function () {
    let that = this
    api.ajax(service.url('categoryGetAll'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
      keyword: this.data.searchVal
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          classfiylist: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  //选择分类
  chooseCat: function(e){
    let category_id = e.currentTarget.dataset.catId;
    let category_name = e.currentTarget.dataset.catName;
    let obj = {
      goods_id: category_id,
      index: this.data.idx,
      name: category_name,
      link_type: 2
    }
    var arr = wx.getStorageSync('goodsLink')
    if (arr) {
      let a = true
      for (var i = 0, len = arr.length; i < len; i++) {
        if (obj.index == arr[i].index) {
          a = true
          arr[i] = obj
        } else {
          a = false
        }
      }
      if (!a) {
        arr.push(obj)
      }
      wx.setStorageSync('goodsLink', arr)
    } else {
      let arr = [obj]
      wx.setStorageSync('goodsLink', arr)
    }

    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})